import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:furaha_mkopo/commonTool/fm_jump_tool.dart';
import 'package:furaha_mkopo/commonTool/fm_language_tool.dart';
import 'package:furaha_mkopo/commonTool/fm_route_tool.dart';
import 'package:furaha_mkopo/commonUI/fm_life_visibility_page.dart';
import 'package:furaha_mkopo/manager/fm_global_manager.dart';
import 'package:furaha_mkopo/page/authentication/fm_identity_auth_page.dart';
import 'package:furaha_mkopo/page/base/fm_base_page_widget.dart';
import 'package:furaha_mkopo/page/home/controller/fm_home_detail_controller.dart';
import 'package:furaha_mkopo/page/home/dialog/fm_auth_completed_dialog.dart';
import 'package:furaha_mkopo/page/home/entity/fm_home_detail_entity.dart';
import 'package:furaha_mkopo/page/home/enum/fm_home_net_enum.dart';
import 'package:furaha_mkopo/page/home/orderDetail/emergency/fm_emergency_page.dart';
import 'package:furaha_mkopo/page/home/orderDetail/job/fm_job_page.dart';
import 'package:furaha_mkopo/page/home/orderDetail/personal/fm_personal_page.dart';
import 'package:geolocator_platform_interface/src/models/position.dart';
import 'package:get/get.dart';


class FMHomeDetailPage extends FMBasePageWidget {
  String? listen;
  String? title;
  FMHomeDetailPage({super.key, this.listen,this.title});


  @override
  State<StatefulWidget> createState() {
    return _FMHomeDetailPageState();
  }
}

class _FMHomeDetailPageState extends FMBasePageState<FMHomeDetailPage> {
  final controller = Get.put(FMHomeDetailController());

  @override
  void initState() {
    super.initState();
    controller.onRequestDetailData(widget.listen,widget.title);
  }

  @override
  Widget build(BuildContext context) {
    return FMLifeVisibilityPage(
      parentContext: context,
      viewWillAppear: (){
        controller.onRequestDetailData(widget.listen,widget.title);
      },
      child: GetBuilder<FMHomeDetailController>(
        builder: (controller) {
          if(controller.detailEntity == null) {
            return Container(
              color:const Color(0xFF54E04E),
            );
          }else {
            return Container(
              color: const Color(0xFF54E04E),
              child: Stack(
                children: [
                  Image.asset(
                    "assets/home/ic_detail_bg.png",
                    fit: BoxFit.fill,
                  ),
                  Column(
                    children: [
                      SizedBox(
                        height: MediaQuery.of(context).viewPadding.top,
                      ),
                      generateTitle(context),
                      Expanded(
                        child: SingleChildScrollView(
                          child: Column(
                          children: [
                            generateContentImage(),
                            generateContent(),
                            const SizedBox(height: 12),
                            if (controller
                                    .detailEntity?.wasted?.soul?.isNotEmpty ==
                                true)
                              generateBottomSelect(
                                  controller.detailEntity?.wasted?.soul ?? "",
                                  controller.detailEntity?.wasted?.millions ??
                                      ""),
                            if (controller
                                    .detailEntity?.wasted?.soul?.isNotEmpty ==
                                true)
                              const SizedBox(height: 8),
                            generateBottomBtn(),
                          ],
                        ),
                      ),
                      ),
                    ],
                  ),
                ],
              ),
            );
          }
        }
      ),
    );
  }

  Widget generateTitle(BuildContext context) {
    String title = controller.detailEntity?.masters?.attempt ?? "";
    return SizedBox(
      height: 44,
      width: double.infinity,
      child: Stack(
        children: [
          Positioned(
              left: 18,
              top: 0,
              bottom: 0,
              child: GestureDetector(
                  behavior: HitTestBehavior.translucent,
                  onTap: () {
                    FMRouteTool.pop(context);
                  },
                  child: Image.asset("assets/images/back.png",
                      width: 24, height: 24))),
          Positioned(
              left: 0,
              top: 0,
              bottom: 0,
              right: 0,
              child: Container(
                alignment: Alignment.center,
                child: Text(
                  title,
                  style: const TextStyle(
                      fontSize: 16,
                      color: Color(0xFF333333),
                      fontWeight: FontWeight.w600,
                      decoration: TextDecoration.none),
                ),
              )),
        ],
      ),
    );
  }

  Widget generateContentImage() {
    return Container(
        margin: const EdgeInsets.only(left: 16, right: 16, top: 4),
        child: Image.asset(
          FMLanguageTool.getAssets("assets/home/ic_detail_content_bg",suffix: ".webp"),
          fit: BoxFit.fill,
        ));
  }

  Widget generateContent() {
    List<Cost> costs = controller.detailEntity?.cost ?? [];
    List<Widget> widgets = [];
    for (int i = 0; i < costs.length; i++) {
      Cost cost = costs[i];
      bool isRight = ((i + 1) % 2) == 0;
      String soul = cost.soul ?? "--";
      widgets.add(GestureDetector(
        onTap: (){
          onTapAuthentication(cost.hunt??"",cost.bidding??"");
        },
        child: Container(
          height: cost.itemHeight ?? 0,
          width: double.infinity,
          margin: const EdgeInsets.only(left: 16, right: 16),
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage(cost.imageBg ?? ''),
              fit: BoxFit.fill,
            ),
          ),
          child: Column(
            children: [
              Container(
                height: 45,
                width: double.infinity,
                alignment: isRight ? Alignment.centerRight : Alignment.centerLeft,
                padding: const EdgeInsets.only(left: 20, right: 20),
                margin: EdgeInsets.only(
                    top: i == 0 ? 16 : (16 + 25), left: 12, right: 12),
                decoration: BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage(cost.imageTitleBg ?? ''),
                    fit: BoxFit.fill,
                  ),
                ),
                child: Text(
                  FMLanguageTool.getText("Step${i + 1}"),
                  style: const TextStyle(
                      color: Colors.black,
                      fontSize: 18,
                      fontWeight: FontWeight.w700,
                      decoration: TextDecoration.none),
                ),
              ),
              Container(
                height: 95,
                width: double.infinity,
                margin: const EdgeInsets.only(top: 10.52, left: 12, right: 12),
                padding: const EdgeInsets.only(left: 12, right: 12),
                decoration: BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage(cost.imageCardBg ?? ''),
                    fit: BoxFit.fill,
                  ),
                ),
                child: Column(
                  crossAxisAlignment:
                      isRight ? CrossAxisAlignment.end : CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      soul,
                      style: const TextStyle(
                          fontSize: 16,
                          color: Color(0xFF333333),
                          fontWeight: FontWeight.w700,
                          decoration: TextDecoration.none),
                    ),
                    const SizedBox(
                      height: 8,
                    ),
                    GestureDetector(
                      behavior: HitTestBehavior.opaque,
                      onTap: (){
                        onTapAuthentication(cost.hunt??"",cost.bidding??"");
                      },
                      child: Container(
                        width: 106,
                        height: 36,
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                          image: DecorationImage(
                            image: AssetImage(cost.imageBtnBg ?? ''),
                            fit: BoxFit.fill,
                          ),
                        ),
                        child: Text(
                          cost.genuine == 1 ? FMLanguageTool.getText("Completed") :FMLanguageTool.getText("Fill in"),
                          style: const TextStyle(
                              fontSize: 14,
                              color: Colors.white,
                              fontWeight: FontWeight.w400,
                              decoration: TextDecoration.none),
                        ),
                      ),
                    )
                  ],
                ),
              )
            ],
          ),
        ),
      ));
    }
    return Column(
      children: widgets,
    );
  }

  Widget generateBottomSelect(String soul,String url) {
    return Row(
      children: [
        const SizedBox(
          width: 38,
        ),
        Image.asset(
          "assets/home/ic_detail_select.png",
          width: 16,
          height: 16,
        ),
        const SizedBox(
          width: 6,
        ),
        Image.asset(
          "assets/home/ic_detail_un_select.png",
          width: 16,
          height: 16,
        ),
        const SizedBox(
          width: 6,
        ),
        Text(soul),
        const SizedBox(
          width: 38,
        ),
      ],
    );
  }

  Widget generateBottomBtn() {
    return GestureDetector(
      onTap: (){
        Navigator.of(context).pop();
        FMJumpTool.jumpHomeWidget();
      },
      child: Container(
        height: 62,
        width: double.infinity,
        alignment: Alignment.center,
        margin: const EdgeInsets.only(left: 30, right: 30, bottom: 12),
        decoration: const BoxDecoration(
          image: DecorationImage(
            image: AssetImage("assets/home/ic_detail_bottom_btn.png"),
            fit: BoxFit.fill,
          ),
        ),
        child: Text(
          (controller.detailEntity?.masters?.passing ?? controller.detailEntity?.passing)??"",
          style: const TextStyle(
              fontSize: 18,
              fontWeight: FontWeight.w700,
              color: Colors.white,
              decoration: TextDecoration.none),
        ),
      ),
    );
  }

  void onTapAuthentication(String tag,String bidding){
    String curListen = widget.listen??"";
    if(curListen.isEmpty){
      curListen = controller.detailEntity?.talked.toString() ?? "";
    }
    if(curListen.isEmpty){
      return;
    }
    FMJumpTool.jumpAuth(context, tag, bidding, curListen, false,isOldPage: true);
  }

  void authCompleted(String bidding){
    showDialog(
        context: context,
        builder: (context) {
          return FMAuthCompletedDialog(
            onConfirm: () {
              // TODO 跳转
              Navigator.of(this.context).pop();
              FMJumpTool.jumpWebWidget(context: this.context, url: bidding);
            },
          );
        });
  }

  @override
  void onLocationError(String error) {
    // TODO: implement onLocationError
  }

  @override
  void onLocationUpdated(Position position) {
    controller.position = position;
  }
}
